// ==UserScript==
// @name TianyaFilter
// @namespace http://bbs.tianya.cn/post-stocks-216255-1.shtml
// @version    1.3.0
// @author     Master Rhino from TianYa BlackSmith's Shop
// @author     Citizen Luther from TianYa BlackSmith's Shop
// @description 天涯社区楼层计数、过滤指定用户的回贴。
// @include http://bbs.tianya.cn/*
// @exclude
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_getResourceText
// @grant GM_addStyle
// @updateURL   https://userscripts.org/scripts/source/169619.meta.js
// @downloadURL https://userscripts.org/scripts/source/169619.user.js
// @require http://code.jquery.com/jquery-1.9.1.js
// @require http://code.jquery.com/ui/1.10.3/jquery-ui.js
// @resource    jqCSS http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css
// @copyright   2013+, Citizen Luther
// Released under the GPL license
//  http://www.gnu.org/copyleft/gpl.html
// Appreciate to
//  Tianya Helper (http://userscripts.org/scripts/show/127574)
// script link: http://userscripts.org/scripts/show/169619
// ver 0.1 @ 2013-6-4
//  Initialize release
// ver 0.2 @ 2013-6-4
//  + 增加选项，可设置过滤模式
//  * 做了一些优化
// ver 1.0 @ 2013-6-6
//  + 增加关注某人功能，关注ID的回贴，设置背景色为环保绿色
//  + 增加数楼层、黑名单屏蔽效果的设置项
//  * 修正因大小写不符导致屏蔽失败的问题
// ver 1.0.1 @ 2013-6-9
//  * 修正用户名中含有特殊字符或英文字符在前导致无法过滤的问题
// ver 1.1.0 @ 2013-6-16
//  + 增加鼠标跟随的快捷链接：拉黑、洗白、关注、取关，用于快捷维护黑白名单
// ver 1.1.1 @ 2013-6-19
//  + 增加“高亮提到我的”选项，默认关闭
// ver 1.2.0 @ 2013-8-14
//  * 修复因天涯升级导致的举报及回复功能失效问题
// ver 1.2.1 @ 2014-7-21
//  * 修复因天涯升级导致的屏蔽功能失效问题
//	* 增加屏蔽回帖中的嵌入帖功能
// ver 1.3.0 @ 2014-7-21
//  * 增加白名单模式
// ==/UserScript==

/*-----------------------------------------------------------------------------
 * Declaration
 *-------------------------------------------------------------------------- */
HIGHLIGHT_COLOR = "#CCE8CF"; //环保背景色 

var TianyaConfig = function () {
    this.blackList = ','; //黑名单
    this.whiteList = ','; //白名单
    this.filterMode = 0; //过滤模式：0-黑名单，黑名单中不显示, 1: 白名单模式，只显示白名单
    this.filterStyle = 1; //过滤样式：0-过滤回帖和用户信息；1-过滤回帖，不过滤用户信息
    this.floorNum = 1;  //数楼层开关：0-关，1-开
    this.focusReplyMe = 0; //是否高亮提到我的回帖：0-否，1-是
    this.init();
};

/* config format: key:writer0,writer1..:status ... */
TianyaConfig.prototype = {
    saveBlackIDs: function () {
        GM_setValue('TianYaBlackIDs', this.blackList);
    },
    __getBlackIDs: function () {
        var escBlackIds = GM_getValue('TianYaBlackIDs');
        if (typeof escBlackIds != "undefined" && escBlackIds != null) {
            this.blackList = escBlackIds;
        }
    },
    saveWhiteIDs: function () {
        GM_setValue('TianYaWhiteIDs', this.whiteList);
    },
    __getWhiteIDs: function () {
        var escWhiteIds = GM_getValue('TianYaWhiteIDs');
        if (typeof escWhiteIds != "undefined" && escWhiteIds != null) {
            this.whiteList = escWhiteIds;
        }
    },
    saveOptions: function () {
        GM_setValue('TianYaFilterMode', this.filterMode);
        GM_setValue('TianYaFilterStyle', this.filterStyle);
        GM_setValue('TianYaFloorNum', this.floorNum);
        GM_setValue('TianYaFocusReplyMe', this.focusReplyMe);
    },
    __getOptions: function () {
        this.filterMode = GM_getValue('TianYaFilterMode','0');
        this.filterStyle = GM_getValue('TianYaFilterStyle','1');
        this.floorNum = GM_getValue('TianYaFloorNum', '1');
        this.focusReplyMe = GM_getValue('TianYaFocusReplyMe', '0');
    },
    init: function () {
        this.__getBlackIDs();
        this.__getWhiteIDs();
        this.__getOptions();
    }
};

/*-----------------------------------------------------------------------------
 * Initialization
 *-------------------------------------------------------------------------- */
if (typeof Tyconfig == 'undefined') {
    Tyconfig = new TianyaConfig();
}

var eleHeader = document.getElementsByClassName("read-menu")[0];
if (typeof eleHeader != 'undefined') {
    var btnBlack = document.createElement("a");
    btnBlack.id = "btn_filter";
    btnBlack.className = "ty_filter";
    btnBlack.href = 'javascript:void(0)';
    btnBlack.textContent = "设置天涯过滤器";
    eleHeader.appendChild(btnBlack);

    var jqCSS = GM_getResourceText("jqCSS");
    GM_addStyle(jqCSS);

    var helpbar = document.createElement('div');
    helpbar.id = "tianya_helpbar";
    helpbar.align = "center";
    helpbar.style.display = "none";
    helpbar.innerHTML =
        '<div id="tabs" align="left">\
           <ul>\
               <li><a href="#tabs-1" style="margin:0">黑名单</a></li>\
               <li><a href="#tabs-2" style="margin:0">白名单</a></li>\
               <li><a href="#tabs-3" style="margin:0">设置</a></li>\
           </ul>\
           <div id="tabs-1">\
               <span>输入需要屏蔽的天涯ID，用逗号分隔，不要有空格：</span><br/>\
               <textarea title="提交后，新增ID会被屏蔽，但从黑名单删除的ID的回贴不会出现，需要F5。" id="txt_black_list" class="tyblackid" rows="5" cols="40" wrap="virtual">\
               </textarea><br/>\
               <button id="btn_submit_black" class="btn_submit">提交</button>\
               <button class="btn_cancel">关闭</button>\
           </div>\
           <div id="tabs-2">\
               <span>输入需要关注的天涯ID，用逗号分隔：</span><br/>\
               <textarea title="白名单ID的回贴，将以环保绿色背景标记。" id="txt_white_list" class="tywhiteid" rows="5" cols="40" wrap="virtual">\
               </textarea><br/>\
               <button id="btn_submit_white" class="btn_submit">提交</button>\
               <button class="btn_cancel">关闭</button>\
           </div>\
           <div id="tabs-3">\
               <span>修改选项保存后，需要刷新页面才在当前页生效。</span><br/>\
               <label>过滤模式：</label><select id="sel_filter_mode"><option value="0">黑名单模式</option> <option value="1">白名单模式</option></select><br>\
               <input id="chk_floor" type="checkbox" title="打勾后将显示天涯楼层"> 页内数楼层</input><br/> \
               <input id="chk_filter_style" type="checkbox" title="黑名单过滤模式，未选则连ID信息一起过滤"> 只过滤回贴内容，保留用户信息</input><br/>\
               <input id="chk_focus_me" type="checkbox" title="以粗体标记提到我的回帖"> 高亮提到我的回复（登录后生效）</input><br/>\
               <br/><br/>\
               <button id="btn_submit_option" class="btn_submit">保存</button>\
               <button class="btn_cancel">关闭</button>\
           </div>\
         </div>';
    eleHeader.appendChild(helpbar);

    GM_addStyle(".btn_submit { padding-top:5px;padding-right:10px;padding-bottom:5px;padding-left:10px; }");
    GM_addStyle(".btn_cancel { padding-top:5px;padding-right:10px;padding-bottom:5px;padding-left:10px; }");

    // Tab switch
    $("#tabs").tabs();

    // Show and initialize Tianya Filter configuration view
    $("#btn_filter").click(function(){
        document.getElementById("tianya_helpbar").style.display = "block";
        $("#tabs").tabs("option", "active", 0);

        document.getElementById("txt_black_list").value = Tyconfig.blackList.substring(1,Tyconfig.blackList.length-1);
        document.getElementById("txt_white_list").value = Tyconfig.whiteList.substring(1,Tyconfig.whiteList.length-1);
        document.getElementById("sel_filter_mode").selectedIndex = Tyconfig.filterMode;
        document.getElementById("chk_filter_style").checked = Tyconfig.filterStyle == 1;
        document.getElementById("chk_floor").checked = Tyconfig.floorNum == 1;
        document.getElementById("chk_focus_me").checked = Tyconfig.focusReplyMe == 1;
    });

    $("#btn_submit_option").click(function(){
        Tyconfig.filterMode = document.getElementById("sel_filter_mode").selectedIndex;
        Tyconfig.filterStyle = document.getElementById("chk_filter_style").checked ? 1 : 0;
        Tyconfig.floorNum = document.getElementById("chk_floor").checked ? 1 : 0;
        Tyconfig.focusReplyMe = document.getElementById("chk_focus_me").checked ? 1 : 0;
        Tyconfig.saveOptions();

        document.getElementById("tianya_helpbar").style.display = "none";
    });

    document.getElementById("btn_submit_black").onclick=function(event){
        submitList(event);
    };

    document.getElementById("btn_submit_white").onclick=function(event){
        submitList(event);
    };

    // Submit the change of black list or white list.
    function submitList(event) {
        var target = event.currentTarget;
        var idList = "";

        if (target.id == "btn_submit_black")
            idList = document.getElementById("txt_black_list").value;
        else if (target.id == "btn_submit_white")
            idList = document.getElementById("txt_white_list").value;

        while (idList.length > 0 && idList.indexOf("，") > -1) {
            idList = idList.replace("，",",");
        }
        if (idList.indexOf(",") > 0 || idList.indexOf(",") == -1) idList = "," + idList;
        if (idList.substring(idList.length - 1, idList.length) != ",") idList = idList + ",";
        idList = idList.toLowerCase();

        if (target.id == "btn_submit_black") {
            Tyconfig.blackList = idList;
            Tyconfig.saveBlackIDs();
        } else if (target.id == "btn_submit_white") {
            Tyconfig.whiteList = idList;
            Tyconfig.saveWhiteIDs();
        }

        document.getElementById("tianya_helpbar").style.display = "none";

        // Filter again after list updated.
        filterPosts();
    }

    $(".btn_cancel").click(function(){
        document.getElementById("tianya_helpbar").style.display = "none";
    });
}


/*-----------------------------------------------------------------------------
 * Main Program
 *-------------------------------------------------------------------------- */
// 此结构中的function，在页面加载完毕才会执行，确保不出错。
$(document).ready(function(){
    addMenuAndCountFloor();

    filterPosts();
});

/*-----------------------------------------------------------------------------
 * Functions
 *-------------------------------------------------------------------------- */

// For every user header, add menu for TianyaFilter, and count the floor number
function addMenuAndCountFloor() {
    //var eHeads = document.getElementsByClassName("atl-head-reply");
    var eHeads = document.getElementsByClassName("atl-info");
    if ($("a.bbs-layer").size() > 0) Tyconfig.floorNum = 0;

    for (i=0; i<eHeads.length; i++){
        var ele = eHeads[i];

        if (Tyconfig.floorNum == 1 && i>0)
            ele.innerHTML = "<span class='floornum'>" + i + "楼</span>" + ele.innerHTML;

        ele = ele.getElementsByTagName("span")[1];
        ele.innerHTML = ele.innerHTML +
            "<a class='tyf_link' tyf_type='1' href='javascript:void(0)'>拉黑</a> " +
            "<a class='tyf_link' tyf_type='2' href='javascript:void(0)'>洗白</a> " +
            "<a class='tyf_link' tyf_type='3' href='javascript:void(0)'>关注</a> " +
            "<a class='tyf_link' tyf_type='4' href='javascript:void(0)'>取关</a>";
    }

    var eIrList = document.getElementsByClassName("ir-list");
    for(i=0; i<eIrList.length; i++) {
        var eLiList = eIrList[i].getElementsByTagName("ul")[0].getElementsByTagName("li");
        for(j=0; j<eLiList.length; j++) {
            var ele = eLiList[j].getElementsByTagName("p")[0];
            ele.innerHTML =
                "<a class='tyf_link' tyf_type='1' href='javascript:void(0)'>拉黑</a> " +
                "<a class='tyf_link' tyf_type='2' href='javascript:void(0)'>洗白</a> " +
                "<a class='tyf_link' tyf_type='3' href='javascript:void(0)'>关注</a> " +
                "<a class='tyf_link' tyf_type='4' href='javascript:void(0)'>取关</a>" + ele.innerHTML;
        }
    }

    GM_addStyle(".tyf_link { color:blue } ");

    $(".tyf_link").click(function() {
        var ftype = $(this).attr("tyf_type");

        var author;
        if($(this).parent().parent().hasClass("atl-info")) {
            author = $(this).parent().parent().find("a").attr("uname").toLowerCase();
        } else {
            author = $(this).parent().parent().attr("_username").toLowerCase();
        }

        switch (ftype) {
            case "1": //拉黑，加入黑名单
                if (Tyconfig.blackList.indexOf(","+author+",") < 0) {
                    Tyconfig.blackList += author + ","
                    Tyconfig.saveBlackIDs();

                    $(this).hide();
                    $(this).parent().find(".tyf_link[tyf_type=2]").show();
                    $(this).parent().find(".tyf_link[tyf_type=3]").hide();
                }
                break;
            case "2": //洗白，从黑名单中删除
                var pos = Tyconfig.blackList.indexOf(","+author+",");
                if ( pos >= 0) {
                    Tyconfig.blackList = Tyconfig.blackList.substr(0,pos) + Tyconfig.blackList.substr(pos + author.length + 1);
                    Tyconfig.saveBlackIDs();

                    $(this).hide();
                    $(this).parent().find(".tyf_link[tyf_type=1]").show();
                    $(this).parent().find(".tyf_link[tyf_type=3]").show();
                }
                break;
            case "3": //关注，加入白名单
                if (Tyconfig.whiteList.indexOf(","+author+",") < 0) {
                    Tyconfig.whiteList += author + ","
                    Tyconfig.saveWhiteIDs();

                    $(this).hide();
                    $(this).parent().find(".tyf_link[tyf_type=1]").hide();
                    $(this).parent().find(".tyf_link[tyf_type=4]").show();
                }
                break;
            case "4": //取关，从白名单中删除
                var pos = Tyconfig.whiteList.indexOf(","+author+",");
                if ( pos >= 0) {
                    Tyconfig.whiteList = Tyconfig.whiteList.substr(0,pos) + Tyconfig.whiteList.substr(pos + author.length + 1);
                    Tyconfig.saveWhiteIDs();

                    $(this).hide();
                    $(this).parent().find(".tyf_link[tyf_type=1]").show();
                    $(this).parent().find(".tyf_link[tyf_type=3]").show();
                }
                break;
        }

        filterPosts();
    }).hide();

//发贴部分 鼠标跟随效果，黑名单ID显示“洗白”，白名单ID显示“取关”，其他ID显示“拉黑、关注”
    $(".atl-info").hover(
        function () {
            var author = $(this).parent().find("a").attr("uname").toLowerCase();
            if (Tyconfig.blackList.indexOf(","+author+",") >= 0)
                $(this).find(".tyf_link[tyf_type=2]").show();
            else if (Tyconfig.whiteList.indexOf(","+author+",") >= 0)
                $(this).find(".tyf_link[tyf_type=4]").show();
            else {
                $(this).find(".tyf_link[tyf_type=1]").show();
                $(this).find(".tyf_link[tyf_type=3]").show();
            }
        },
        function () {
            $(this).find(".tyf_link").hide();
        }
    );

//发帖下方评论区 鼠标跟随效果，黑名单ID显示“洗白”，白名单ID显示“取关”，其他ID显示“拉黑、关注”
    $(".ir-list").find("ul").find("li").hover(
        function () {
            var author = $(this).attr("_username").toLowerCase();
            if (Tyconfig.blackList.indexOf(","+author+",") >= 0)
                $(this).find(".tyf_link[tyf_type=2]").show();
            else if (Tyconfig.whiteList.indexOf(","+author+",") >= 0)
                $(this).find(".tyf_link[tyf_type=4]").show();
            else {
                $(this).find(".tyf_link[tyf_type=1]").show();
                $(this).find(".tyf_link[tyf_type=3]").show();
            }
        },
        function () {
            $(this).find(".tyf_link").hide();
        }
    );
}

// Filter posts of black IDs, and highlight posts of focused IDs.
function filterPosts() {
    //过滤帖子内容

    //黑名单模式
    if(Tyconfig.filterMode == 0) {
        filterPostsOfBlackIds();
    } else {
        filterPostsExceptWhiteIds();
    }

    // 高亮提到自己的回复，需要登录后才能使用
    if (Tyconfig.focusReplyMe == 1) {
        var myid = $("a[appstr='mypage']").text();
        if (typeof myid != "undefined" && myid != ""){
            $(".bbs-content:contains('" + myid + "')").css("font-weight", "bold");
        }
    }
}

function filterPostsOfBlackIds() {
    var eItems = document.getElementsByClassName("atl-item");
    for (i=0; i<eItems.length; i++){
        var ele = eItems[i];
        var username = ele.getElementsByClassName("js-vip-check")[0].getAttribute("uname");
        if (username == null) continue;

        username = username.toLowerCase();

        if (Tyconfig.blackList.indexOf(","+username+",")>=0){
            //屏蔽回贴模式，只隐藏回贴，否则将包含用户信息的整个div隐藏
            if (Tyconfig.filterStyle == 1) {
                var post = ele.getElementsByClassName("atl-content")[0];
                post.style.display = "none";
            } else {
                ele.style.display = "none";
            }
        } else if (Tyconfig.whiteList.indexOf(","+username+",") >= 0){ //关注ID的回贴，设置背景色为环保绿色
            var cell = ele.getElementsByClassName("atl-con-bd clearfix")[0].getElementsByClassName("bbs-content")[0];
            cell.style.backgroundColor = HIGHLIGHT_COLOR;
        }

        //

        var lis = ele.getElementsByTagName("li");
        for (j=0; j<lis.length; j++){
            var li = lis[j];
            username = li.getAttribute("_username");
            if (username == null) continue;

            username = username.toLowerCase();

            if (Tyconfig.blackList.indexOf(","+username+",")>=0){
                li.style.display = "none";
            } else if (Tyconfig.whiteList.indexOf(","+username+",") >= 0){ //关注ID的嵌入回贴，设置背景色为环保绿色
                li.style.backgroundColor = HIGHLIGHT_COLOR;
            }
        }
    }
}

function filterPostsExceptWhiteIds() {
    var eItems = document.getElementsByClassName("atl-item");
    for (i=0; i<eItems.length; i++){
        var ele = eItems[i];
        var username = ele.getElementsByClassName("js-vip-check")[0].getAttribute("uname");
        if (username == null) continue;

        username = username.toLowerCase();

        if (Tyconfig.whiteList.indexOf(","+username+",") >= 0){ //关注ID的回贴，设置背景色为环保绿色
            var cell = ele.getElementsByClassName("atl-con-bd clearfix")[0].getElementsByClassName("bbs-content")[0];
            cell.style.backgroundColor = HIGHLIGHT_COLOR;

            ele.getElementsByClassName("atl-content")[0].style.display = "block";
            ele.getElementsByClassName("atl-con-bd clearfix")[0].style.display = "block";
        } else {
            //屏蔽回贴模式，只隐藏回贴，否则将包含用户信息的整个div隐藏
            if (Tyconfig.filterStyle == 1) {
                var post = ele.getElementsByClassName("atl-content")[0];
                post.style.display = "none";
            } else {
                ele.style.display = "none";
            }
        }

        //

        var lis = ele.getElementsByTagName("li");
        for (j=0; j<lis.length; j++){
            var li = lis[j];
            username = li.getAttribute("_username");
            if (username == null) continue;

            username = username.toLowerCase();

            if (Tyconfig.whiteList.indexOf(","+username+",") >= 0){ //关注ID的嵌入回贴，设置背景色为环保绿色
                li.style.backgroundColor = HIGHLIGHT_COLOR;
            } else {
                li.style.display = "none";
            }
        }
    }
}